#include "emb_res.h"
#include "ui_emb_res.h"
#include "dhlz_type.h"
#include "package.h"
#include "about.h"
#include "help_outfile.h"
#include <QDebug>
#include <QStatusBar>

#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QMessageBox>
#include <QFileDialog>

emb_res::emb_res(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::emb_res)
{
    ui->setupUi(this);
    // 创建菜单栏
    QMenuBar *menuBar = new QMenuBar(this);
    // 创建菜单
    QMenu *editMenu = new QMenu("编辑", this);
    QMenu *helpMenu = new QMenu("帮助", this);
    // 添加菜单到菜单栏
    menuBar->addMenu(editMenu);
    menuBar->addMenu(helpMenu);
    // 创建动作（Actions）
    QAction *edit_addall = new QAction("导入目录文件", this);
    QAction *edit_add = new QAction("导入单文件", this);
    QAction *edit_dele = new QAction("清除全部", this);
    // 将动作添加到菜单中
    editMenu->addAction(edit_addall);
    editMenu->addAction(edit_add);
    editMenu->addSeparator(); // 添加分隔符
    editMenu->addAction(edit_dele);
    QAction *help_about = new QAction("关于", this);
    QAction *help_outfile = new QAction("输出头文件", this);
    helpMenu->addAction(help_about);
    helpMenu->addAction(help_outfile);
    filelist.set(this, 5, 26);
    in_dialog.set(ui->groupBox, 5, 50, NULL, "导入");
    out_dialog.set(ui->groupBox, 5, 95, NULL, "输出");
    stbar = new QStatusBar(this);
    stbar->resize(750, 25);
    stbar->move(10, 805);
    connect(&in_dialog, SIGNAL(ctrl_Signal()), this, SLOT(in_dialog_sloot()));
    connect(&out_dialog, SIGNAL(ctrl_Signal()), this, SLOT(out_dialog_sloot()));
    connect(edit_addall, &QAction::triggered, this, &emb_res::in_dialog_sloot);
    connect(edit_dele, &QAction::triggered, this, &emb_res::delete_all_sloot);
    connect(help_about, &QAction::triggered, this, &emb_res::help_about_sloot);
    connect(help_outfile, &QAction::triggered, this, &emb_res::help_outfile_sloot);
}

emb_res::~emb_res()
{
    delete ui;
}

void emb_res::in_dialog_sloot()
{
    if (in_dialog.is_path())
    {
        filelist.set_path(in_dialog.get_path());
    }
    else
    {
        statusbar_sloot("导入路径失败");
    }
}

void emb_res::out_dialog_sloot()
{
    if (filelist.get_count() == 0)
    {
        statusbar_sloot("请先导入文件");
        return;
    }

    if (out_dialog.is_path())
    {
        package pack;
        pack.reset(out_dialog.get_path());
        pack.set_offset(filelist.get_offset());
        pack.set_count(filelist.get_count());

        for (int i = 0; i < filelist.get_count(); i++)
        {
            pack.push(i, filelist.get_index(i));
        }

        if (pack.check(ui->Edit_Ver->text()))
        {
            statusbar_sloot("输出成功");
        }
        else
        {
            statusbar_sloot("输出失败");
        }
    }
    else
    {
        statusbar_sloot("输出路径异常");
    }
}

void emb_res::delete_all_sloot()
{
    if (filelist.get_count())
    {
        filelist.reset();
        statusbar_sloot("清除成功");
    }
    else
    {
        statusbar_sloot("清除失败");
    }
}

void emb_res::filelist_sloot(int)
{
    unsigned short size = (sizeof(struct file_info_t) + 0x07) & 0x0FF8;
    size = ((sizeof(struct file_head_t) + 0x0F) & 0x7F0) + size * filelist.get_count();

    if (filelist.get_offset() < size)
    {
        filelist.set_offset(filelist.get_offset() + 1024);
    }
}

void emb_res::help_outfile_sloot()
{
    if (out_dialog.is_path())
    {
        QFileInfo fileInfo(out_dialog.get_path());
        QString path = fileInfo.absolutePath();// 提取路径部分
        help_outfile outfile;

        if (outfile.path(path))
        {
            statusbar_sloot("已输出到路径：" + path);
        }
        else
        {
            statusbar_sloot("输出头文件失败");
        }
    }
    else
    {
        statusbar_sloot("输出路径无效");
    }
}

void emb_res::help_about_sloot()
{
    about *abou = new about(this);
    abou->show();
}

void emb_res::statusbar_sloot(QString qstr)
{
    stbar->showMessage(qstr, 500);
}
